<?php

declare(strict_types=1);

namespace app\controller;

use Firebase\JWT\JWT;
use think\facade\Db;
use think\Request;
// use app\controller\m
// use think\Controller;
// use think\Request;

use app\model\User as UserModel;
use app\model\UserAccount as UserAccount;
use app\model\TempCode as TempCode;

class Login extends Base
{

    // 登录
    public function login()
    {
        $data = request()->param();
        // return $data;
        $user_model = new UserModel();
        $info = $user_model->getInfoByWorkNum($data['work_num']);
        // return $info;
        if ($info[0] === false) {
            return $this->create('', '没有这个工号', 204);
        }else if($info[1]['status']==0||$info[1]['status']==2){
            return $this->create(['code'=>2], '账户未激活或者已锁定');
        }
        $token = setToken($info[1]);
        $returndata = [
            'roles' => (int)$info[1]['roles'],
            'work_num' => $info[1]['work_num'],
            'token' => $token,
            'code' =>1
        ];
        // return ['token'=>$token];
        return $this->create($returndata, '成功');
    }
    
    // 获取登录数据
    public function getinfo(Request $request)
    {
        $token_data = (array)$request->data['data'];
        // return $token_data;
        // return $token_data;
        // $token = $request->param();
        // $token = $request->header('x-Token');
        // $payload = checkToken($token);
        // $id = $payload->work_num;
        $user_model = new UserModel();
        $account_model = new UserAccount();
        $avatar = $account_model->getInfoByWorkNum($token_data['work_num'])[1]['avatar'];
        $info = $user_model->getInfoByWorkNum($token_data['work_num'])[1];
        $data = [
            'name'   => $info['name'],
            'roles'  => [$info['roles']],
            'avatar' => $avatar,
            'sex'    => $info['sex'],
            'age'    => $info['age'],
            'position'    => $info['position'],
            'department'    => $info['department'],
            'create_time'    => $info['create_time'],
            'email'    => $info['email'],
            'work_num'    => $info['work_num'],
        ];
        // return 123;
        return $this->create($data, '成功');
    }
    // 注册
    public function register()
    {
        // return $this->create('','成功');

        $data = request()->param();
        $token = request()->header('x-Token');
        $key = "api";
        $payload = JWT::decode($token, $key, array('HS256'));
        Db::name('user')->insert($data);
        return $this->create('', '成功');
    }

    /**
     * @description: 发送登录码
     * @param {type} 
     * @return {type} 
     */
    public function sendLoginCode()
    {
        $post = request()->param();
        // return $post;
        // $post =  request()->param();
        //PHP获得随机数-验证码
        $v_code = rand(111111, 999999);
        //PHP获取时间戳
        $time = time();
        //拼接时间戳与登录码
        $log_code = (string)$time . (string)$v_code;
        $user_model = new UserModel();
        $code_model = new TempCode();
        //查询账户对应email
        $user_res = $user_model->getInfoByWorkNum($post['work_num']);
        if ($user_res[0] === true) {
            $user_info = $user_res[1];
        } else {
            return $this->create('', '找不到用户', 204);
        }
        //删除之前的登录码
        $code_model->deleteCode($post['work_num']);
        //保存登录码信息到临时表
        $res =  $code_model->saveCode($post['work_num'], $log_code);
        //字符串截取指定片段
        // $v_code = substr($log_code, 10, 6);
        // $person_email = $person_model->getInfoByNumber($post['number'], 'email');
        $title = '验证码';
        // $data = json_decode($string, true);
        // $content = $v_code;
        // $content=emailHtmlModel($person_info['name'],$v_code,'登录' );
        // return $this->create($content);

        // $content = '你好, <b>' . $person_info['name'] . '</b>管理员! <br/>这是一封来自河池学院党支部的邮件！<br/><span>你正在登录管理员账户,你的验证码是:' . (string)$v_code;
        // return $user_info;
        $content = '你好, <b>高级用户</b>! <br/><br/><span>你的验证码是:' . (string)$v_code;
        if ($res === true) {
            if (sendMail($user_info['email'], $title, $content)) {
                $code = 20000;
                $msg = '发送成功';
            } else {
                $code = 204;
                $msg = '发送失败';
            }
        } else {
            $code = 204;
            $msg = $res;
        }
        return $this->create('', $msg, $code);
    }

    public function checkLoginCode()
    {
        $post = request()->param();
        // $account_model = new UserAccount();
        $code_model = new TempCode();

        // $user_res = $account_model->getInfoByWorkNum($post['work_num'])[1];
        $code = $code_model->getInfoByWorkNum($post['work_num'])[1]['code'];
        $v_code = substr($code, 10, 6);
        // return [$v_code,$post['code']];
        if($v_code != $post['code']){
            return $this->create(['code'=>0], '验证失败');
        }

        $user_model = new UserModel();
        $info = $user_model->getInfoByWorkNum($post['work_num']);
        $token = setToken($info[1]);
        $returndata = [
            'token' => $token,
            'code'  => 1
        ];
        // return ['token'=>$token];
        return $this->create($returndata, '成功');
    }

    public function logout()
    {
        return $this->create(null, '成功');
    }


    public function test()
    {
        return sendMail('2045596421@qq.com', 'person', 'hello');
    }
}
